package com;

import java.util.Date;
import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 线程状态转换测试
 * @author MaoLin Wang
 * @description
 * @date 2020/9/5 10:39 上午
 */
public class ThreadState {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        /*Object obj=new Object();

        new Thread(()->{
            synchronized (obj){
                try {
                    System.out.println("线程1调用wait()");
                    obj.wait();
                    System.out.println("线程1结束wait()");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"thread——1").start();

        new Thread(()->{
            synchronized (obj){
                System.out.println("线程2调用notify");
                obj.notify();
                System.out.println("线程2结束nofity");
               while (true){

               }
            }
        },"thread——2").start();*/

       /* ReentrantLock lock=new ReentrantLock();
        Condition condition = lock.newCondition();
        Thread thread1 = new Thread(() -> {
            try {
                System.out.println("线程1调用lock.lock()");
                lock.lock();
                System.out.println("线程1结束lock.lock()");
                System.out.println("线程1调用await");
                condition.await();
                System.out.println("线程1结束调用await");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println("线程1调用unlock");
                lock.unlock();
            }
        }, "thread——1");
        thread1.start();

        Thread thread = new Thread(() -> {
            try {
                System.out.println("线程2调用lock");
                lock.lock();
                System.out.println("线程2结束调用lock");
                System.out.println("线程2调用signal");
                condition.signal();
                System.out.println("线程2结束调用signal");

                Thread.sleep(1000000000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "thread——2");
        thread.start();
        thread1.join();*/

        ScheduledThreadPoolExecutor executor=new ScheduledThreadPoolExecutor(2,new ThreadPoolExecutor.CallerRunsPolicy());
      /*  executor.schedule(()->{
            System.out.println("lala"+ System.currentTimeMillis());
        },2, TimeUnit.SECONDS);

       ScheduledFuture<String>future= executor.schedule(()->{
            System.out.println("heih"+System.currentTimeMillis());
            return "1";
        },2,TimeUnit.SECONDS);
        System.out.println(future.get());*/

        executor.scheduleWithFixedDelay(()->{
            System.out.println("bibi"+new Date());
            try {
                Thread.sleep(3000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },0,2,TimeUnit.SECONDS);

        String str1= new String("a");
        String str2= "a";
        String str3= "a";
        System.out.println(str1 == str2) ;//false
        System.out.println(str2 == str3) ;//true
    }


}
